import { defineStore } from 'pinia'
import { ref, computed } from 'vue'
import { onLoad } from '@dcloudio/uni-app'
import api from '@/api'
import configRouter from '@/config/config.router'
import configApp from '@/config/config.app.js'
import cache from '@/utils/cache'
import { wechat } from '@/hooks/wechat'
const { getWechatLoginCode } = wechat()

export const useAuthStore = defineStore('auth', () => {
    const redirectUrl = ref('')
    const access_token = ref(cache.get(configApp.accessToken))
    const refresh_token = ref(cache.get(configApp.refreshToken))
    const userInfo = ref(cache.get('userInfo'))
    const isLogin = computed(() => {
        return access_token.value && refresh_token.value
    })

    // 设置跳转登录的来源页
    onLoad((options) => {
        if (options.redirectUrl) {
            redirectUrl.value = decodeURIComponent(options.redirectUrl)
        }
        // #ifdef MP-WEIXIN
        if (getCurrentPages().length === 1) {
            uni.hideHomeButton()
        }
        // #endif
    })

    // 获取用户信息
    async function getUserInfo() {
		const userInfo = {
			nickName: '用户',
			realName: '张三',
		}
		setUserInfo(userInfo, { cover: true })
    }

    // 设置用户信息
    function setUserInfo(data, options = {}) {
        const { cover = false, path } = options
        if (cover) {
            userInfo.value = data
        } else if (path) {
            setProperty(userInfo.value, path, data)
        } else {
            userInfo.value = { ...userInfo.value, ...data }
        }
        cache.set('userInfo', userInfo.value)
        // 返回更新后的 userInfo.value
        return userInfo.value
    }

    // 清除用户信息
    function clearUserInfo() {
        userInfo.value = null
        access_token.value = null
        refresh_token.value = null
        cache.remove('userInfo')
        cache.remove(configApp.accessToken)
        cache.remove(configApp.refreshToken)
        uni.reLaunch({
            url: configRouter.loginPage,
        })
    }

    // 登录
    async function login(data = {}, type) {
        try {
            let res
            if (type == 'wechat') {
                const code = await getWechatLoginCode()
                // 请求登录接口赋值给res
            }
            if (type == 'pwd') {
                
            }
			// 设置登录状态
			cache.set(configApp.accessToken, '123456')
			cache.set(configApp.refreshToken, '123456')
            // 获取用户信息
            await getUserInfo()
            // 跳转来源页
            loginBack()
        } catch (error) {
            console.log('登录错误', error)
        }
    }

    // 登出
    async function logout() {
        clearUserInfo()
    }

    // 跳转来源页
    function loginBack() {
        if (redirectUrl.value) {
            uni.reLaunch({
                url: redirectUrl.value,
            })
        } else {
            uni.reLaunch({
                url: configRouter.homePage,
            })
        }
    }

    return {
        redirectUrl,
        access_token,
        refresh_token,
        userInfo,
        isLogin,
        getUserInfo,
        setUserInfo,
        clearUserInfo,
        login,
        logout,
        loginBack,
    }
})
